from django.shortcuts import render

from django.http import HttpResponse,JsonResponse
from rest_framework.request import Request
from utils.db import db1


# 查询
def showuserudit(Request):
    #     查询客户申请表
    sql = "select c.id,c.name,c.tmoney,c.company_name,c.status from customer as c left join reg_form as rf on c.refromid=rf.id left join customer_manager as cm on cm.id=c.manager_id"

    res = db1.find_all(sql)
    return JsonResponse({'code':200,'list':res})


# 点击更改
import json
def audit(requerst):
    import time
    # 获取参数
    data= json.loads(requerst.body)
    time = int(time.time())
    #                      客户申请表
    ssql = "select * from customer where id=%d"%(int(data['id']))
    customer = db1.find(ssql)

    # 更新  客户申请表 constmer表  状态  审核人  审核时间
    sql = "update customer set status=%d,audit_time=%d,audit_name='梦儿' where id=%d" % (
    int(data['is_agree']), time, int(data['id']))
    db1.update(sql)

    # 更新表资源                    注册来源表
    sql1 = "select id,tcount from reg_form where id=%d" % (int(customer['refromid']))
    regform = db1.find(sql1)
    number = int(regform['tcount']) + 1
    sql2 = 'update reg_form set tcount=%d where id=%d' % (int(number), int(customer['refromid']))
    db1.update(sql2)
    # 更新客户经理中的总金额     客户经理表
    sql3 = "select * from customer_manager where id=%d" % (int(customer['manager_id']))
    manager = db1.find(sql3)
    number = int(manager['tmoney']) + int(customer['tmoney'])

    sql4 = 'update customer_manager set tmoney=%d where id=%d' % (int(number), int(customer['manager_id']))
    db1.update(sql4)
    return JsonResponse({"code": 200})




# 查询echarts展示数据
def getmes(request):
    sql = "select name,tcount from reg_form"
    res = db1.find_all(sql)
    list = [{'value': i['tcount'], 'name': i['name']} for i in res]
    return JsonResponse({'code':200,'list':list})

# 查询客户信息表所有数据
def getcustomer(request):
    sql = 'select * from customer'
    res = db1.find_all(sql)
    return JsonResponse({"code":200,'list':res})





from dwebsocket.decorators import accept_websocket

websocketlist = []

@accept_websocket
def websocket_conn(request):
    if request.is_websocket:
        websocketlist.append(request.websocket)

    for message in request.websocket:
        break

    # 发送消息
def sendmes():
    sql = "select name from customer"
    res = db1.find_all(sql)
    if websocketlist:
        list = []
        for i in websocketlist:
            for j in res:
                list.append({'name':j['name']})
            b1 = json.dumps(list, ensure_ascii=False).encode('utf-8')
            i.send(b1)
    return HttpResponse('ok')



from apscheduler.schedulers.background import BackgroundScheduler
def test(request):
     scheduler = BackgroundScheduler()
     # 添加调度任务
     # 调度方法为 timedTask，触发器选择 interval(间隔性)，间隔时长为 2 秒
     scheduler.add_job(sendmes, 'interval', seconds=1)
     # 启动调度任务
     scheduler.start()














